package com.simple.weibo.oauth;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;

import com.simple.weibo.bean.User;

public class OAuth {

	public static  String appKey = "4275878347";
	public static String appSecret="18a3484a5b52cffa23460f5e87ad686f";
	
	private String requestUrl = "http://api.t.sina.com.cn/oauth/request_token";
	private String accessUrl = "http://api.t.sina.com.cn/oauth/access_token";
	private String authorizeUrl = "http://api.t.sina.com.cn/oauth/authorize";
	
	private String callbackUrl = "myapp://OAuthActivity";
	
	private OAuthConsumer consumer;
	private OAuthProvider provider;
	
	public OAuth(){
		consumer = new CommonsHttpOAuthConsumer(appKey, appSecret);
		provider = new CommonsHttpOAuthProvider(requestUrl, accessUrl, authorizeUrl);
		
	}
	
	public boolean requestAccessToken(Activity ay,WebView wv){
		
		boolean flag = false;
		try {
			String url = provider.retrieveRequestToken(consumer, callbackUrl);
			flag = true;
									
			wv.loadUrl(url);
	    	
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
		
	}
	
	public User getAccessToken(Intent intent){
		
		User user = null;
		
		Uri uri = intent.getData();
		String verificationCOde = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
		try {
			provider.setOAuth10a(true);
			provider.retrieveAccessToken(consumer, verificationCOde);
			user = new User();
			user.setUserId(provider.getResponseParameters().get("user_id").first());
			user.setAccessToken(consumer.getToken());
			user.setAccessTokenSecret(consumer.getTokenSecret());
			System.out.println(consumer.getToken());
			System.out.println(consumer.getTokenSecret());
			System.out.println(provider.getResponseParameters().get("user_id").first());
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthNotAuthorizedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
		
	}
	
	public HttpResponse getServerResponse(String token,String tokenSecret,String url,List<NameValuePair>  pairList){
		
			HttpResponse response = null;
		
			consumer.setTokenWithSecret(token,tokenSecret);
			HttpPost request = new HttpPost(url);
			HttpClient cilent = new DefaultHttpClient();
			try {
				request.setEntity(new UrlEncodedFormEntity(pairList,"UTF-8"));
				request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
				consumer.sign(request);
				response = cilent.execute(request);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OAuthMessageSignerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OAuthExpectationFailedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OAuthCommunicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return response;
	}
}
